package cn.hchaojie.camp.javafx.controls;

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class DemoTableView2 extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		Scene scene = new Scene(createContents());
		
		stage.setScene(scene);
		stage.show();
	}
	
	// 在这里创建页面布局
	private Parent createContents() {
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(10));
		grid.setVgap(10);
		grid.setHgap(10);
		
		Label labelGroup = new Label("分组");
		grid.add(labelGroup, 0, 0);
		GridPane.setHalignment(labelGroup, HPos.CENTER);
		
		Label labelContacts = new Label("联系人");
		grid.add(labelContacts, 1, 0);
		GridPane.setHalignment(labelContacts, HPos.CENTER);
		
		ListView<Group> groupList = createListView();
		grid.add(groupList, 0, 1);	
		groupList.getItems().addAll(createContacts());

		TableView<Contact> tableView = createTableView();
		grid.add(tableView, 1, 1);

		// 注册ListView选择项改变时的处理器
		groupList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Group>() {
			@Override
			public void changed(ObservableValue<? extends Group> observable, Group oldValue, Group newValue) {
				// 可能没选择任何一个
				if (newValue != null) {
					tableView.getItems().clear();
					tableView.getItems().addAll(newValue.getContacts());
				}
			}
		});
		
		// ListView默认选择第一个，这里之所以清除选择，再重新选上第一个
		// 主要是为了触发上面注册的选择改变事件
		groupList.getSelectionModel().clearSelection(0);
		groupList.getSelectionModel().selectFirst();
		
		return grid;
	}
	
	private static ListView<Group> createListView() {
		ObservableList<Group> groups = FXCollections.observableArrayList();
		ListView<Group> groupList = new ListView<>();
		groupList.setPrefWidth(150);
		groupList.setItems(groups);
		groupList.getSelectionModel().selectFirst();
		return groupList;
	}
	
	/* 创建表格 TableView */
	private static TableView<Contact> createTableView() {
		TableView<Contact> tableView = new TableView<>();
		
		tableView.setPrefWidth(400);

		final ObservableList<Contact> contacts = FXCollections.observableArrayList();
		tableView.setItems(contacts);

		TableColumn<Contact, String> aliasNameCol = new TableColumn<>("姓名");
		aliasNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String>("name"));
		aliasNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		TableColumn<Contact, String> firstNameCol = new TableColumn<>("电话");
		firstNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String>("phone"));
		firstNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		TableColumn<Contact, String> lastNameCol = new TableColumn<>("备注");
		lastNameCol.setCellValueFactory(new PropertyValueFactory<Contact, String> ("note"));
		lastNameCol.setPrefWidth(tableView.getPrefWidth() / 3);

		ObservableList<TableColumn<Contact, ?>> columns = tableView.getColumns();
		columns.add(aliasNameCol);
		columns.add(firstNameCol);
		columns.add(lastNameCol);
		
		return tableView;
	}
	
	private static List<Group> createContacts() {
		List<Group> groups = new ArrayList<>();
		
		List<Contact> contacts = ContactsData.getContacts();
		
		Group g1 = new Group("家人");
		g1.getContacts().addAll(contacts.subList(0, 10));
		groups.add(g1);

		Group g2 = new Group("同事");
		g2.getContacts().addAll(contacts.subList(11, 40));
		groups.add(g2);

		Group g3 = new Group("朋友");
		g3.getContacts().addAll(contacts.subList(40, 50));
		groups.add(g3);

		Group g4 = new Group("基佬");
		g4.getContacts().addAll(contacts.subList(50, 60));
		groups.add(g4);
		
		return groups;
	}

	public static void main(String[] args) {
		launch(args);
	}
}
